﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AppHelper;

namespace Data
{
    public class Collections
    {
        private static DataDataContext dc = null;

        public static DataDataContext Dc
        {
            get
            {
                if (dc == null)
                {
                    dc = new DataDataContext(Common.GetConnectionString());
                }
                return dc;
            }
        }

        #region Musteri
        // Müşteri ile ilgili Kodlar burada yazılacak. Müşteri silme, Ekleme, Güncelleme gibi..
        public static List<MUSTERILER> GetMusteriList()
        {
            List<MUSTERILER> entity = new List<MUSTERILER>();
            entity = (from p in Dc.MUSTERILERs
                      select p).ToList<MUSTERILER>();
            return entity;
        }

        public static MUSTERILER GetMusteri(int MusteriID)
        {
            MUSTERILER entity = new MUSTERILER();
            entity = (from p in Dc.MUSTERILERs
                      where p.MusteriID == MusteriID
                      select p).FirstOrDefault<MUSTERILER>();
            return entity;
        }

        public static MUSTERILER GetMusteri(string Email)
        {
            MUSTERILER entity = new MUSTERILER();
            entity = (from p in Dc.MUSTERILERs
                      where p.Email == Email
                      select p).FirstOrDefault<MUSTERILER>();
            return entity;
        }

        public static bool DeleteMusteri(MUSTERILER value)
        {
            bool deger = false;
            if (Dc.MUSTERILERs.Where(a => a.MusteriID == value.MusteriID).Count() == 0)
            {
                dc.MUSTERILERs.DeleteOnSubmit(value);
                dc.SubmitChanges();
                deger = true;
            }
            return deger;
        }

        public static void SaveMusteri(MUSTERILER value)
        {
            if (value.MusteriID == 0)
                Dc.MUSTERILERs.InsertOnSubmit(value);
            Dc.SubmitChanges();
        }


        public static MUSTERILER GetMusteri(string Email, string Sifre)
        {
            MUSTERILER entity = new MUSTERILER();
            entity = (from p in Dc.MUSTERILERs
                      where p.Email == Email
                      && p.Sifre == Sifre
                      select p).FirstOrDefault<MUSTERILER>();

            return entity;
        }


        #endregion

        #region Personel
        public static PERSONEL GetPersonel(int PersonelId)
        {
            PERSONEL entity = new PERSONEL();
            entity = (from p in Dc.PERSONELs
                      where p.Yetki == "Amir"
                      select p).FirstOrDefault<PERSONEL>();
            return entity;
        }
        #endregion

        #region Ürünler
        public static List<URUNLER> GetUrunList()
        {
            List<URUNLER> entity = new List<URUNLER>();
            entity = (from p in Dc.URUNLERs
                      select p).ToList<URUNLER>();

            return entity;
        }
        #endregion

        #region İller
        public static SEHIR GetSehir(int SehirID)
        {
            SEHIR entity = new SEHIR();
            entity = (from p in Dc.SEHIRs
                      where p.SehirID == SehirID
                      select p).FirstOrDefault<SEHIR>();
            return entity;
        }
        public static List<SEHIR> GetSehirList()
        {
            List<SEHIR> entity = new List<SEHIR>();
            entity = (from p in Dc.SEHIRs
                      orderby p.SehirID
                      select p).ToList<SEHIR>();
            return entity;
        }

        public static void SaveSehir(SEHIR entity)
        {
            if (entity.SehirID == 0)
            {
                dc.SEHIRs.InsertOnSubmit(entity);
                Dc.SubmitChanges();
            }
        }
        public static bool DeleteSehir(SEHIR entity)
        {
            bool deger = false;
            if (!Dc.SEHIRs.Where(a => a.SehirID == entity.SehirID).Any())
            {
                Dc.SEHIRs.DeleteOnSubmit(entity);
                Dc.SubmitChanges();
                deger = true;
            }
            return deger;


        }
        #endregion

        #region Subscription

        public static SUBSCRIPTION GetSubscription(string Email)
        {
            SUBSCRIPTION entity = new SUBSCRIPTION();
            entity = (from p in Dc.SUBSCRIPTIONs
                      where p.EMail == Email
                      select p).FirstOrDefault<SUBSCRIPTION>();
            return entity;
        }

        public static bool IsSubscribed(string Email)
        {
            bool deger = false;
            if (Dc.SUBSCRIPTIONs.Where(a => a.EMail == Email).Count() != 0)
            {
                deger = true;
            }
            return deger;
        }
        public static List<SUBSCRIPTION> GetSubscriptionList()
        {
            List<SUBSCRIPTION> entity = new List<SUBSCRIPTION>();
            entity = (from p in Dc.SUBSCRIPTIONs
                      select p).ToList<SUBSCRIPTION>();
            return entity;
        }

        public static void DeleteSubscription(SUBSCRIPTION value)
        {
            Dc.SUBSCRIPTIONs.DeleteOnSubmit(value);
            Dc.SubmitChanges();
        }

        public static void SaveSubscription(SUBSCRIPTION value)
        {
            if (value.SubscriptionID == 0)
            {
                Dc.SUBSCRIPTIONs.InsertOnSubmit(value);
            }
            Dc.SubmitChanges();
        }

        #endregion
    }
}
